package com.liang.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liang.auth.api.entity.Result;
import com.liang.circle.api.req.ShareCircleRemoveReq;
import com.liang.circle.api.req.ShareCircleSaveReq;
import com.liang.circle.api.req.ShareCircleUpdateReq;
import com.liang.circle.api.vo.ShareCircleVO;
import com.liang.circle.server.entity.po.ShareCircle;
import com.liang.circle.server.service.ShareCircleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/circle/share/circle")
@RequiredArgsConstructor
public class ShareCircleController {

    private final ShareCircleService shareCircleService;

    /**
     * 新增圈子
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody ShareCircleSaveReq requestParam) {
        try {
            log.info("新增圈子入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getCircleName(), "圈子名称不能为空");
            Preconditions.checkNotNull(requestParam.getIcon(), "圈子图标不能为空");
            if (requestParam.getParentId() != -1) {
                ShareCircle circle = shareCircleService.getById(requestParam.getParentId());
                Preconditions.checkNotNull(circle, "父级圈子不存在");
            }

            shareCircleService.saveCircle(requestParam);
            return Result.ok();
        } catch (Exception e) {
            log.error("新增圈子异常！错误原因{}", e.getMessage(), e);
            return Result.fail("新增圈子异常！");
        }
    }

    /**
     * 修改圈子
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody ShareCircleUpdateReq requestParam) {
        try {
            log.info("修改圈子入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getCircleName(), "圈子名称不能为空");
            Preconditions.checkNotNull(requestParam.getIcon(), "圈子图标不能为空");
            if (requestParam.getParentId() != -1) {
                ShareCircle circle = shareCircleService.getById(requestParam.getParentId());
                Preconditions.checkNotNull(circle, "父级圈子不存在");
            }

            shareCircleService.updateCircle(requestParam);
            return Result.ok("修改圈子信息成功");
        } catch (Exception e) {
            log.error("修改圈子异常！错误原因{}", e.getMessage(), e);
            return Result.fail("修改圈子异常！");
        }
    }

    /**
     * 删除圈子
     */
    @PostMapping("/remove")
    public Result<Void> remove(@RequestBody ShareCircleRemoveReq requestParam) {
        try {
            log.info("删除圈子入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getId(), "圈子ID不能为空");

            shareCircleService.removeCircle(requestParam.getId());
            return Result.ok("删除圈子信息成功");
        } catch (Exception e) {
            log.error("删除圈子异常！错误原因：{}", e.getMessage(), e);
            return Result.fail("删除圈子异常！");
        }
    }

    /**
     * 查询圈子
     */
    @GetMapping("/list")
    public Result<List<ShareCircleVO>> listCircle() {
        List<ShareCircleVO> res = shareCircleService.listCircle();
        return Result.ok(res);
    }
}
